package com.dlxj.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

public class MyJSON {

	public static String toJSONStr(List<HashMap<String, String>> data,
			final String... propertyNames) {
		String str = "[";

		for (HashMap<String, String> hashMap : data) {
			str += "{";
			for (int i = 0; i < propertyNames.length; i++) {
				String key = propertyNames[i];
				String value = hashMap.get(key);
				if (value != null && value.length() != 0) {
					str += "\"" + key + "\"" + " : " + "\"" + value + "\",";
				}
			}
			if (",".equals(str.substring(str.length() - 1))) {

				str = str.substring(0, str.length() - 1);
			}
			str += "\n},";

		}
		if (",".equals(str.substring(str.length() - 1))) {

			str = str.substring(0, str.length() - 1);
		}
		str += "]";

		return str;

	}

	/**
	 * 屏蔽不需要转换为json 的属性
	 * 
	 * @param obj
	 * @param propertyNames
	 * @return
	 */
	public static JSON toJson(Object obj, final String... propertyNames) {

		JSON msg = null;

		// 声明配置项
		JsonConfig config = new JsonConfig();

		// 屏蔽掉hibernate关联生成的属性
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });

		// 注册日期转换器
		config.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		config.registerJsonValueProcessor(Timestamp.class,
				new JsonDateValueProcessor());

		// 过滤不需要的属性
		if (propertyNames != null && propertyNames.length > 0) {

			config.setJsonPropertyFilter(new PropertyFilter() {

				public boolean apply(Object arg0, String arg1, Object arg2) {

					// 循环匹配不需要的属性，如果符合，跳出（return true），不进行json转换
					for (String str : propertyNames) {
						if (arg1.equals(str)) {
							return true;
						}
					}
					return false;

				}
			});

		}

		msg = JSONSerializer.toJSON(obj, config);

		return msg;
	}

	public static String toJsonStr(Object obj, final String... propertyNames) {
		return toJson(obj, propertyNames).toString();
	}

	public static JSON toJson(Object obj, int pattern,
			final String... propertyNames) {

		JSON msg = null;

		// 声明配置项
		JsonConfig config = new JsonConfig();

		// 屏蔽掉hibernate关联生成的属性
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });

		// 注册日期转换器
		config.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));

		// 过滤不需要的属性
		if (propertyNames != null && propertyNames.length > 0) {

			config.setJsonPropertyFilter(new PropertyFilter() {

				public boolean apply(Object arg0, String arg1, Object arg2) {

					// 循环匹配不需要的属性，如果符合，跳出（return true），不进行json转换
					for (String str : propertyNames) {
						if (arg1.equals(str)) {
							return true;
						}
					}
					return false;

				}
			});

		}

		msg = JSONSerializer.toJSON(obj, config);

		return msg;
	}

}
